/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javateknik.portal.model.entities;

import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author hanssonke
 */
public class MessageDAO {

    private EntityManager entityManager;

    public EntityManager getEntityManager() {
        if (entityManager == null) {
            EntityManagerFactory factory;
            factory = Persistence.createEntityManagerFactory("portal");
            entityManager = factory.createEntityManager();
        }
        return entityManager;
    }

    public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }

    public void saveMessage(Message message) {
        getEntityManager().getTransaction().begin();
        getEntityManager().persist(message);
        getEntityManager().getTransaction().commit();
    }

    public List<Message> getMessageList(){
        return getEntityManager().createNamedQuery("findAllMessages").getResultList();
    }
}
